package zozopo.user;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.comm.Validate;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

public class RegistAction extends ActionSupport implements Action ,SessionAware,ServletRequestAware{
	
	private static final Log logger = LogFactory.getLog(RegistAction.class);
	
	private Map<Object, Object> session;
	private HttpServletRequest request;
	
	
	private String username;
	private String password;
	private String valicode;
	private String email;
	private String qq;
	
	private String comfirmpassword;
	
	private UserTB user;
	
	public String getComfirmpassword() {
		return comfirmpassword;
	}

	public void setComfirmpassword(String comfirmpassword) {
		this.comfirmpassword = comfirmpassword;
	}



	private String vilicode;
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getValicode() {
		return valicode;
	}

	public void setValicode(String valicode) {
		this.valicode = valicode;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getVilicode() {
		return vilicode;
	}

	public void setVilicode(String vilicode) {
		this.vilicode = vilicode;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		this.session = arg0;
	}
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}
	
	
	
	public String execute(){
		String forward = "error";
		UserOS userOS = new UserOS();
		user = new UserTB();
		
		user.setEmail_user(email);
		user.setName_user(username);
		user.setPassword_user(password);
		user.setQq_user(qq);
		
		if(userOS.registUser(user)){
			forward = "success";
		}
		
		user = userOS.getUserInfoByName(username);
		
		try {
			userOS.createActiveNumber(user);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			userOS.releaseConnection();
		}
		
		
		return forward;
		
	}
	
	public void validate() {
		// 若debug模式开启，则输出debug 信息
        if (logger.isDebugEnabled()) {
            logger.debug(getUsername() +" "+getPassword() + " " + getValicode());
        }
      
		ActionContext actionContext = ActionContext.getContext(); 
		
		//验证码
		boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, valicode);
		
		if(!captchaPassed){
			addFieldError("errormessage",getText("background.login.valicode.iswrong"));
			return;
		}
		
		if(username.length() > 20){
			addFieldError("errormessage","用户名称不能大于20个字符,请重新输入");
			return;
		}
		
		if(qq.length() > 32){
			addFieldError("errormessage","QQ号码不能大于32个字符,请重新输入");
			return;
		}
		
		if(! Validate.isEmail(email)){
			addFieldError("errormessage","e-mail格式错误请输入正确的e-mail");
			return;
		}
		
		if(email.length() > 320){
			addFieldError("errormessage","email不能大于320个字符,请重新输入");
			return;
		}
		
		if(!password.equals(comfirmpassword)){
			addFieldError("errormessage","输入两次密码不一致请重新输入");
			return;
		}
		
		
		String content = username + email+ qq + password;
		if(Validate.isSpecialCharacters(content)){
			addFieldError("errormessage","不能输入特殊符号  ;\"'\\/*&`+-%?!<>[]{}|");
			return;
		}
		
		if(!Validate.isNumber(qq)){
			addFieldError("errormessage","qq只能是数字");
			return;
		}
		
		UserOS userOS = new UserOS();
		try {
			if(userOS.isExist(username)){
				addFieldError("errormessage","用名已存在");
				return;
			}
			
			if(userOS.isLockedEmail(email)){
				addFieldError("errormessage","email 地址已经被使用");
				return;
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		userOS.releaseConnection();
	}

}
